package com.lebang.service.setting;

import com.lebang.mapper.setting.CashMapper;
import com.lebang.model.enums.Booleans;
import com.lebang.model.setting.Cash;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CashService {
    @Autowired
    private CashMapper cashMapper;

    public Map<String, Object> pagging(Cash vo) {
        Long total = cashMapper.total();
        List<Cash> rows = cashMapper.pagging(vo);
        Map<String, Object> data = new HashMap<>(1);
        data.put("total", total);
        data.put("rows", rows);
        return data;
    }

    public JsonData edit(Cash vo) {
        try {
            vo.setModifyTime(new Date());
            BigDecimal i = vo.getCashMoney();
            int r = i.compareTo(new BigDecimal("00.01"));
            int j = i.compareTo(new BigDecimal("100000"));
            i = i.setScale(2, BigDecimal.ROUND_DOWN);
            vo.setCashMoney(i);
            if (j > 0) {
                return Callback.fail("200", "非法金额！修改失败", null);
            }
            if (r < 0) {
                return Callback.fail("200", "非法金额！修改失败", null);
            }
        } catch (Exception e) {
            return Callback.fail("200", "非法金额！修改失败", null);
        }
        vo.setHasDeleted(Booleans.False.name());
        if (null != vo.getId()) {
            int o = cashMapper.selectCountByMoney(vo.getCashMoney(), vo.getId());
            if (o > 0) {
                return Callback.fail("200", "金额已存在", null);
            }
            int i = cashMapper.updateByPrimaryKeySelective(vo);
            if (i > 0) {
                return Callback.success("200", "修改成功", null);
            } else {
                return Callback.fail("200", "修改失败", null);
            }
        } else {
            int k = cashMapper.selectCountByCashMoney(vo.getCashMoney());
            if (k > 0) {
                return Callback.fail("200", "金额已存在", null);
            }
            int l = cashMapper.findSumCountByCash();
            if (l > 10) {
                return Callback.fail("200", "最多设置10条", null);
            }
            int i = cashMapper.insertSelective(vo);
            if (i > 0) {
                return Callback.success("200", "新增成功", null);
            } else {
                return Callback.fail("200", "新增失败", null);
            }
        }
    }

    public JsonData del(Integer id) {
        if (null != id) {
            int i = cashMapper.deleteByPrimaryKey(id);
            if (i > 0) {
                return Callback.success("200", "删除成功", null);
            } else {
                return Callback.fail("200", "删除失败", null);
            }
        } else {
            return Callback.fail("200", "删除失败", null);
        }
    }

    public Cash selectByPrimaryKey(Integer id) {
        return cashMapper.selectByPrimaryKey(id);
    }

    public List<BigDecimal> findCashList() {
        try {
            return cashMapper.findCashList();
        } catch (Exception e) {
            return null;

        }

    }

    /**
     * 最小的提现金额
     *
     * @return
     */
    public BigDecimal selectMinCash() {
        return cashMapper.selectMinCash();
    }

    public List<Cash> findlist() {

        return cashMapper.findlist();
    }
}
